package sesi.ejava.stream;

import sesi.ejava.bcam.R;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ServerSenderActivity extends Activity {

	public static final int REQUEST_ENABLE_BLUETOOTH = 1;

	protected static final int TMP = 0;
	protected static final int SPD = 1;
	protected static final int RPM = 2;
	protected static final int OIL = 3;
	protected static final int GAS = 4;
	protected static final int GAR = 5;

	private BluetoothAdapter bluetoothAdapter;
	private ServerService serverService;
	private Button sendButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.server_sender_activity);

		// Get local Bluetooth adapter
		bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

		// If the adapter is null, then Bluetooth is not supported
		if (bluetoothAdapter == null) {
			Toast.makeText(this, "Bluetooth is not available",
					Toast.LENGTH_LONG).show();
			finish();
			return;
		}
	}

	@Override
	protected void onStart() {
		super.onStart();

		// If BT is not on, request that it be enabled.
		// setupChat() will then be called during onActivityResult
		if (!bluetoothAdapter.isEnabled()) {
			Intent enableIntent = new Intent(
					BluetoothAdapter.ACTION_REQUEST_ENABLE);
			startActivityForResult(enableIntent, REQUEST_ENABLE_BLUETOOTH);
			// Otherwise, setup the chat session
		} else {
			if (serverService == null) {
				setupServer();
			}
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (requestCode == REQUEST_ENABLE_BLUETOOTH) {
			// When the request to enable Bluetooth returns
			if (resultCode == Activity.RESULT_OK) {
				// Bluetooth is now enabled, so set up a chat session
				setupServer();
			} else {
				// User did not enable Bluetooth or an error occured
				Toast.makeText(this, "Bluetooth non activé", Toast.LENGTH_SHORT)
						.show();
				finish();
			}
		}
	}

	private void ensureDiscoverable() {
		if (bluetoothAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
			Intent discoverableIntent = new Intent(
					BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
			discoverableIntent.putExtra(
					BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
			startActivity(discoverableIntent);
		}
	}

	private void setupServer() {
		// Initialize the send button with a listener that for click events
		sendButton = (Button) findViewById(R.id.buttonSend);
		sendButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// Send a message using content of the edit text widget
				TextView txtGas = (TextView) findViewById(R.id.editTextGas);
				TextView txtOil = (TextView) findViewById(R.id.editTextOil);
				TextView txtTemperature = (TextView) findViewById(R.id.editTextTemperature);
				TextView txtRPM = (TextView) findViewById(R.id.editTextRPM);
				TextView txtSpeed = (TextView) findViewById(R.id.editTextSpeed);
				TextView txtGear = (TextView) findViewById(R.id.editTextGear);

				byte[] send = new byte[6];
				send[GAS] = (byte) Integer
						.parseInt(txtGas.getText().toString());
				send[OIL] = (byte) Integer
						.parseInt(txtOil.getText().toString());
				send[TMP] = (byte) Integer.parseInt(txtTemperature.getText()
						.toString());
				send[RPM] = (byte) Integer
						.parseInt(txtRPM.getText().toString());
				send[SPD] = (byte) Integer.parseInt(txtSpeed.getText()
						.toString());
				send[GAR] = (byte) Integer.parseInt(txtGear.getText()
						.toString());

				serverService.write(send);
			}
		});

		//serverService = new ServerService(this);
		serverService = ServerService.getInstance();
		if (serverService.getState() == ServerService.STATE_NONE) {
			ensureDiscoverable();
			serverService.start();
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
}
